package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SySResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin //跨域访问
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;
    @PostMapping("/login")
    public SySResult login(@RequestBody User user){
        //查询数据库得到的一个密钥
       String token = service.selectUserByUP(user);
       if(token == null || "".equals(token)){
           //用户名和密码错误
           return SySResult.fail();
       }
       //表示密码和用户名正确，返回密钥信息
       return SySResult.success(token);
    }
    @GetMapping("/list")
    //获取用户列表并进行分页
    public SySResult getUserList(PageResult pageResult){
        pageResult = service.getUserList(pageResult);
        return SySResult.success(pageResult);
    }
    //根据id修改用户的状态restFel请求
  /*   /user/status/{id}/{status}
  *    在这里我们用对象来封装*/
    @PutMapping("/status/{id}/{status}")
    public SySResult updateStatusById(User user){
        service.updateStatusById(user);
        return SySResult.success();
    }
    @PostMapping("/addUser")
    public SySResult addUser(@RequestBody User user){
        service.addUser(user);
        return SySResult.success();
    }
    @GetMapping("/{id}")
    public SySResult findUserById(@PathVariable Integer id){
        User user = service.findUserById(id);
        return SySResult.success(user);
    }
    @PutMapping("/updateUser")
    public SySResult updateUserById(@RequestBody User user){
        service.updateUserById(user);
        return SySResult.success();
    }
    @DeleteMapping("/{id}")
    public SySResult deleteUserById(@PathVariable Integer id){
        service.deleteUserById(id);
        return SySResult.success();
    }
}
